//
//  MainViewController.swift
//  weibo
//
//  Created by zz on 16/2/21.
//  Copyright © 2016年 zz. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //MARK:- 视图生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        print(NSHomeDirectory())
        setUptabbar()
        
        showNewFeature()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composeButton)
        //防边界处理
        let w = UIScreen.mainScreen().bounds.width / CGFloat(childViewControllers.count)-1
        composeButton.frame = CGRectInset(tabBar.bounds, 2*w, 0)
        
    }
    //MARK:- 监听方法
    @objc private func didClickComposeButton() {
        print("点击了撰写按钮")
    }
    
    //MARK:- 私有控件
    private lazy var composeButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
}

//MARK:- 新特性处理
extension MainViewController {
    
    ///显示新特性
    private func showNewFeature() {
        
        if !UserAccount.sharedUserAccount.isLogin{
            return
        }
        //到这代表用户已经登录了,如果有新版本,显示新特性界面,不然显示欢迎界面
        let v =  isNewVersion() ? NewFeatureView() : WelcomeView()
        view.addSubview(v)
    }
    
    ///是否有新版本
    private func isNewVersion() -> Bool{
        //获取当前版本
        let currentVersion = NSBundle.cz_currentVersion
        //获取沙盒存储的版本
        let key = "LEOSanboxVersion"
        let sandboxVersion = (NSUserDefaults.standardUserDefaults().objectForKey(key) ?? "0.0.0") as? String
        //将当前版本存储在沙盒中
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: key)
        return sandboxVersion != currentVersion
        
    }
}

//MARK: - 设置tabbar
extension MainViewController{
    
    func setUptabbar() {
        tabBar.shadowImage = UIImage.leo_DrawDotImage(UIColor(white: 0.8, alpha: 0.9))
        tabBar.backgroundImage = UIImage(named:"tabbar_background")
        tabBar.addSubview(composeButton)
        composeButton.addTarget(self, action: "didClickComposeButton", forControlEvents: .TouchUpInside)
    }
}

//MARK:-  添加控制器
extension MainViewController{
    ///添加所有的子控制器
    func addChildViewControllers() {
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    ///添加一个控制器
    func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
}